package com.octopus.kernal.util.xml;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.octopus.kernal.ui.element.BaseElement;
import com.octopus.kernal.ui.element.CheckboxElement;
import com.octopus.kernal.ui.element.DateElement;
import com.octopus.kernal.ui.element.InputElement;
import com.octopus.kernal.ui.element.SelectElement;
import com.octopus.kernal.ui.element.TextareaElement;
import com.octopus.kernal.ui.element.TimeElement;

@XmlType(name="ElementSearch")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class ElementSearch {
	
	@XmlAttribute
	private String id;
	
	@XmlAttribute
	private String name;
	
	@XmlAttribute
	private String layout = "1|1";
	
	private Integer layoutLabel;
	
	private Integer layoutValue;
	
	@XmlElement(name = "InputElement")
	private InputElement inputElement;

	@XmlElement(name = "DateElement")
	private DateElement dateElement;
	
	@XmlElement(name = "TimeElement")
	private TimeElement timeElement;
	
	@XmlElement(name = "TextareaElement")
	private TextareaElement textareaElement;
	
	@XmlElement(name = "SelectElement")
	private SelectElement selectElement;
	
	@XmlElement(name = "CheckboxElement")
	private CheckboxElement checkboxElement;

	private void init(BaseElement e){
		if(e.getId() == null)
			e.setId(id);
		if(e.getName() == null)
			e.setName(name);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getLayout() {
		return layout;
	}

	public void setLayout(String layout) {
		this.layout = layout;
	}

	public Integer getLayoutLabel() {
		if(layoutLabel == null && StringUtils.isNotBlank(layout) && layout.indexOf("|") > 0){
			String l = layout.substring(0, layout.indexOf("|"));
			if(StringUtils.isNotBlank(l) && NumberUtils.isNumber(l)){
				layoutLabel = Integer.valueOf(l);
			}
		}
		return null==layoutLabel?1:layoutLabel;
	}

	public void setLayoutLabel(Integer layoutLabel) {
		this.layoutLabel = layoutLabel;
	}

	public Integer getLayoutValue() {
		if(layoutValue == null && StringUtils.isNotBlank(layout) && layout.indexOf("|") > 0){
			String l = layout.substring(layout.indexOf("|") + 1);
			if(StringUtils.isNotBlank(l) && NumberUtils.isNumber(l)){
				layoutValue = Integer.valueOf(l);
			}
		}
		return null==layoutValue?1:layoutValue;
	}

	public void setLayoutValue(Integer layoutValue) {
		this.layoutValue = layoutValue;
	}

	public InputElement getInputElement() {
		if(inputElement != null){
			init(inputElement);
			return inputElement.clone();
		}
		return inputElement;
	}

	public void setInputElement(InputElement inputElement) {
		this.inputElement = inputElement;
	}
	
	public DateElement getDateElement() {
		if(dateElement != null){
			init(dateElement);
			return dateElement.clone();
		}
		return dateElement;
	}

	public void setDateElement(DateElement dateElement) {
		this.dateElement = dateElement;
	}

	public TimeElement getTimeElement() {
		if(timeElement != null){
			init(timeElement);
			return timeElement.clone();
		}
		return timeElement;
	}

	public void setTimeElement(TimeElement timeElement) {
		this.timeElement = timeElement;
	}

	public TextareaElement getTextareaElement() {
		if(textareaElement != null){
			init(textareaElement);
			return textareaElement.clone();
		}
		return textareaElement;
	}

	public void setTextareaElement(TextareaElement textareaElement) {
		this.textareaElement = textareaElement;
	}

	public SelectElement getSelectElement() {
		if(selectElement != null){
			init(selectElement);
			return selectElement.clone();
		}
		return selectElement;
	}

	public void setSelectElement(SelectElement selectElement) {
		this.selectElement = selectElement;
	}

	public CheckboxElement getCheckboxElement() {
		if(checkboxElement != null){
			init(checkboxElement);
			return checkboxElement.clone();
		}
		return checkboxElement;
	}

	public void setCheckboxElement(CheckboxElement checkboxElement) {
		this.checkboxElement = checkboxElement;
	}

}
